package com.qhx.rtkcloud.tcp;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

public  class NtripExchangePair implements InitializingBean {


    @Autowired
    private NtripArrayConfig  arrayConfig;

    private Map<String, TcpExchangeUnit> ntripExchangeUnitMap = new ConcurrentHashMap<>();

    private void buildPairs() {
        for (List<TcpConnectProperty> tcpConnectPropertyList : arrayConfig.getUnits()) {
            TcpConnectProperty tcpConnectProperty = tcpConnectPropertyList.get(0);
            TcpConnectProperty tcpConnectPropertyTwo = tcpConnectPropertyList.get(1);
            TcpExchangeUnit ntripExchangeUnit1 = new TcpExchangeUnit(new LinkedBlockingQueue<>());
            TcpExchangeUnit ntripExchangeUnit2 = new TcpExchangeUnit(new LinkedBlockingQueue<>());
            ntripExchangeUnit1.setReadQueue(ntripExchangeUnit2.getWriteQueue());
            ntripExchangeUnit2.setReadQueue(ntripExchangeUnit1.getWriteQueue());
            ntripExchangeUnit1.setConnectFreedom(tcpConnectProperty.isConnectFreedom());
            ntripExchangeUnit2.setConnectFreedom(tcpConnectPropertyTwo.isConnectFreedom());
            ntripExchangeUnit1.setWriteFreedom(tcpConnectProperty.isWriteFreedom());
            ntripExchangeUnit2.setWriteFreedom(tcpConnectPropertyTwo.isWriteFreedom());
            ntripExchangeUnit1.setMount(tcpConnectProperty.getMount());
            ntripExchangeUnit2.setMount(tcpConnectPropertyTwo.getMount());
            ntripExchangeUnit1.setPartner(ntripExchangeUnit2);
            ntripExchangeUnit2.setPartner(ntripExchangeUnit1);
            ntripExchangeUnit1.setAuthType(tcpConnectProperty.getAuthType());
            ntripExchangeUnit2.setAuthType(tcpConnectPropertyTwo.getAuthType());
            ntripExchangeUnitMap.put(tcpConnectProperty.getMount(), ntripExchangeUnit1);
            ntripExchangeUnitMap.put(tcpConnectPropertyTwo.getMount(), ntripExchangeUnit2);

            connectServer(ntripExchangeUnit1, tcpConnectProperty.getHost(), tcpConnectProperty.getPort());
            connectServer(ntripExchangeUnit2, tcpConnectPropertyTwo.getHost(), tcpConnectPropertyTwo.getPort());
        }
    }

    public void connectServer(TcpExchangeUnit ntripExchangeUnit1, String host, int port) {
    }

    public NtripArrayConfig getArrayConfig() {
        return arrayConfig;
    }

    public void setArrayConfig(NtripArrayConfig arrayConfig) {
        this.arrayConfig = arrayConfig;
    }

    public Map<String, TcpExchangeUnit> getNtripExchangeUnitMap() {
        return ntripExchangeUnitMap;
    }

    public void setNtripExchangeUnitMap(Map<String, TcpExchangeUnit> ntripExchangeUnitMap) {
        this.ntripExchangeUnitMap = ntripExchangeUnitMap;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        buildPairs();

    }
}
